package com.pearl.sharding.algorithm.demo.controller;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pearl.sharding.algorithm.demo.entity.OrderEntity;
import com.pearl.sharding.algorithm.demo.service.OrderService;
import org.apache.calcite.avatica.util.DateTimeUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import lombok.RequiredArgsConstructor;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 * 订单信息表 前端控制器
 * </p>
 *
 * @author 自动代码生成
 * @since 2024-01-24
 */
@RestController
@RequestMapping("/order")
@RequiredArgsConstructor
public class OrderController {

    private final OrderService orderService;

    @GetMapping("/list")
    public List<OrderEntity> list() {
        return orderService.list();
    }

    @GetMapping("/page")
    public Object page() {
        return orderService.page(new Page<>(1, 10));
    }


    private static volatile Long ID = 0L;

    @GetMapping("/save")
    public Object save() {
        OrderEntity orderEntity = new OrderEntity();
        ID += 1;
        orderEntity.setId(ID);
        orderEntity.setPhone(18888888888L);
        orderEntity.setAddress("湖南长沙");
        orderEntity.setOrderTime(LocalDateTimeUtil.format(LocalDateTime.now(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        orderEntity.setProductId(1L);
        orderService.save(orderEntity);
        return "操作成功";
    }

    @GetMapping("/getById")
    public Object getById(@RequestParam Long id) {
        return orderService.getById(id);
    }

    @GetMapping("/getByRange")
    public Object getByRange() {
        // ID 集合查询
        LambdaQueryWrapper<OrderEntity> queryWrapper = Wrappers.lambdaQuery();
        orderService.listByIds(ListUtil.toList(11l));
        // 时间范围查询
        queryWrapper.clear();
        queryWrapper.between(OrderEntity::getOrderTime, "2024-02-19 00:00:00", "2024-02-20 00:00:00");
        orderService.list(queryWrapper);
        return "查询成功";
    }
}